<?php
##################
# INSTRUCTIONS
#
# 1) Check if you has PHP installed as **CGI** or as an **Apache-Mod**.
# Create a PHP file with a single line : phpinfo();
# Load that file in a browser, and check if PHP uses CGI or an Apache Module
#
# 2) If you have PHP as CGI
# Uncomment (remove the trailing #) the following line :
# !/usr/local/bin/php -q
# Change the path to your "php" executable, if needed
# Create a crontab : crontab -e (it will open vi editor)
# OR Edit /etc/crontab and add the following line (with all the start, without the # ; see below 4):
# * * * * * php /path/to/your/cron.php
# Change script permissions to 755:
# sudo chmod 755 /path/to/your/cron.php
# Run the following command in the console:
# crontab crontab
#
# 3) If you have PHP as an Apache-Mod
# Install Lynx or wget or curl if you don't already have them (use curl on Mac)
# Create a crontab : crontab -e (if will open vi editor)
# OR Edit /etc/crontab and add one of those lines (see below, 4):
# * * * * * lynx -dump http://www.yoursite.com/path/to/script.php
# * * * * * /usr/bin/curl -0 http://www.yoursite.com/path/to/script.php
# * * * * * /usr/bin/wget -q -O /dev/null http://www.yoursite.com/path/to/script.php
# Refresh the cronjobs with this command:
# crontab crontab
#
# 4) How to configure the cron job ?
# See: http://en.wikipedia.org/wiki/Cron
# 
# *    *    *    *    *      command to be executed
# -    -    -    -    -
# |    |    |    |    |
# |    |    |    |    +----- day of week (0 - 6) (Sunday=0)
# |    |    |    +---------- month (1 - 12)
# |    |    +--------------- day of month (1 - 31)
# |    +-------------------- hour (0 - 23)
# +------------------------- min (0 - 59)
#
# Some exemples:
# * * * * * * php myscript.php #every minute
# * 23 * * * php myscript.php #at 23pm, every day
# * 3,6,9,12 * * * php myscript.php #at 3am, 6am, 9am and 12, every day
# * 8-20 * * * php myscript.php #every hour between 8am and 20pm, every day
# 5 * * * * php myscript.php #every 5 minutes
#
# Useful commands:
# - crontab -e : create a new cron job and open vi editor
# - crontab -l : lists all cron jobs
# - crontab -r : remove a cron job
#
#############

############# 
# Call core libraries and some magic
header('Content-type: text/html; charset=UTF-8'); 
include('../functions.inc.php');


#############
# Do the Job
echo "<p>Updating tags...</p>";
// Stats
$countLoops = 0;
$countUpdates = 0;
$countAdded = 0;
// Loop Articles
$articlesSet = Articles::load("all");
foreach($articlesSet->getArticles() as $article)
{
	//echo "<p>Updating article #" . $article->getId() . "</p>";
	$newTags = array();
	// Match the TITLE against all tags
	$title = $article->getTitle();
	$tagSet = Tag::matchTags($title);
	$newTags = $article->addTagSet($tagSet);
	// Display results
	if(count($newTags) > 0)
	{
		$assoc = '';
		foreach($newTags as $tn)
			$assoc .= $tn . " ";
		echo utf8_encode("<p>Article <b>$title</b> associated with <i>$assoc</i></p>");
		$countUpdates++;
		$countAdded = $countAdded + count($newTags);
	}
	$countLoops++;
}
echo "<p><b>$countLoops</b> articles processed<br /><b>$countUpdates</b> articles updated<br /><b>$countAdded</b> tags added</p>";
echo "<p>Done updating tags.</p>";

?>